package com.auth.manager.service.sys.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.auth.manager.common.ex.BizException;
import com.auth.manager.mapper.sys.SysRoleMapper;
import com.auth.manager.pojo.sys.SysRole;
import com.auth.manager.service.sys.SysRoleService;
import com.auth.manager.utils.page.PageUtil;
import com.auth.manager.utils.page.PageVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @version 1.0.0
 * @Date: 2022/4/28 20:51
 * @Author ZhuYouBin
 * @Description 系统角色
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Override
    public void createRole(SysRole sysRole) {
        // 校验角色标识是否唯一
        QueryWrapper<SysRole> wrapper = new QueryWrapper<>();
        wrapper.eq("role_key", StrUtil.trim(sysRole.getRoleKey()));
        wrapper.eq("deleted", "01");
        long count = this.count(wrapper);
        if (count > 0) {
            throw new BizException("角色标识已存在");
        }
        sysRole.setCreateTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        // 新增数据
        this.save(sysRole);
    }

    @Override
    public void removeRole(String rid) {
        // 查询角色
        SysRole sysRole = this.getById(rid);
        if (Objects.isNull(sysRole)) {
            throw new BizException("用户角色不存在");
        }
        sysRole.setDeleted("02");
        // 更新数据
        this.saveOrUpdate(sysRole);
    }

    @Override
    public void updateRole(SysRole sysRole) {
        // 查询角色
        SysRole currentSysRole = this.getById(sysRole.getRid());
        if (Objects.isNull(currentSysRole)) {
            throw new BizException("用户角色不存在");
        }
        // 校验角色标识是否唯一
        QueryWrapper<SysRole> wrapper = new QueryWrapper<>();
        wrapper.eq("role_key", StrUtil.trim(sysRole.getRoleKey()));
        wrapper.eq("deleted", "01");
        wrapper.ne("rid", currentSysRole.getRid());
        long count = this.count(wrapper);
        if (count > 0) {
            throw new BizException("角色标识已存在");
        }
        // 更新数据
        this.saveOrUpdate(sysRole);
    }

    @Override
    public PageVo queryPage(SysRole sysRole, PageVo pageVo) {
        // 开启分页
        PageUtil.start(pageVo);
        sysRole.setDeleted("01");
        List<SysRole> list = sysRoleMapper.queryByCondition(sysRole);
        return PageUtil.end(list);
    }

    @Override
    public List<SysRole> listAllRole() {
        QueryWrapper<SysRole> wrapper = new QueryWrapper<>();
        wrapper.eq("deleted", "01");
        return this.list(wrapper);
    }

    @Override
    public List<SysRole> queryUserRole(String uid) {
        return sysRoleMapper.queryUserRoleByUid(uid);
    }

    @Override
    public List<SysRole> queryAllRoleAndUrl() {
        return sysRoleMapper.queryAllRoleAndUrl();
    }

}
